/*******************************************************************************
 * Copyright (c) 2009 Dop Sun.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Dop Sun - initial API and implementation
 ******************************************************************************/
package com.softao.ming.gde.internal.ui.preferences;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.preference.IPreferenceStore;

import com.softao.ming.gde.internal.ui.GdeUI;

/**
 * Manage installed GWT in the preference store.
 */
public class InstalledGwt {
	/**
	 * Defines the GWT installation.
	 */
	public static class GwtDefinition {
		private boolean mDefault;
		private String mName;
		private String mPath;
		private String mType;
		/**
		 * @param _default the default to set
		 */
		public void setDefault(boolean _default) {
			mDefault = _default;
		}
		/**
		 * @return the default
		 */
		public boolean isDefault() {
			return mDefault;
		}
		/**
		 * @param name the name to set
		 */
		public void setName(String name) {
			mName = name;
		}
		/**
		 * @return the name
		 */
		public String getName() {
			return mName;
		}
		/**
		 * @param path the path to set
		 */
		public void setPath(String path) {
			mPath = path;
		}
		/**
		 * @return the path
		 */
		public String getPath() {
			return mPath;
		}
		/**
		 * @param type the type to set
		 */
		public void setType(String type) {
			mType = type;
		}
		/**
		 * @return the type
		 */
		public String getType() {
			return mType;
		}
	}

	private static final String PREFIX = "installedGWTs.";
	
	/**
	 * Removes all installed GWT definitions from the preferences.
	 */
	public static void clear() {
		IPreferenceStore store = GdeUI.getDefault().getPreferenceStore();
		
		int i = 0;
		String name = store.getString(PREFIX + "name." + i);
		while (name != null && name.length() > 0) {
			store.setValue(PREFIX + "path." + i, "");
			store.setValue(PREFIX + "name." + i, "");
			store.setValue(PREFIX + "type." + i, "");
			store.setValue(PREFIX + "default." + i, false);
			
			i++;
			name = store.getString(PREFIX + "name." + i);
		}
	}
	
	/**
	 * @return GWT definition list from the preferences.
	 */
	public static List<GwtDefinition> load() {
		IPreferenceStore store = GdeUI.getDefault().getPreferenceStore();
		int i = 0;
		List<GwtDefinition> result = new ArrayList<GwtDefinition>();

		String name = store.getString(PREFIX + "name." + i);
		while (name != null && name.length() > 0) {
			String path = store.getString(PREFIX + "path." + i);
			String type = store.getString(PREFIX + "type." + i);
			boolean isDefault = store.getBoolean(PREFIX + "default." + i);
			GwtDefinition gwtItem = new GwtDefinition();
			gwtItem.mName = name;
			gwtItem.mPath = path;
			gwtItem.mType = type;
			gwtItem.mDefault = isDefault;
			result.add(gwtItem);
			
			i++;
			name = store.getString(PREFIX + "name." + i);
		}
		
		return result;
	}
	
	/**
	 * @param gwts
	 */
	public static void store(List<GwtDefinition> gwts) {
		clear();
		
		IPreferenceStore store = GdeUI.getDefault().getPreferenceStore();
		
		int i = 0;
		for (GwtDefinition item : gwts) {
			store.setValue(PREFIX + "path." + i, item.mPath);
			store.setValue(PREFIX + "name." + i, item.mName);
			store.setValue(PREFIX + "type." + i, item.mType);
			store.setValue(PREFIX + "default." + i, item.mDefault);
			i++;
		}
	}
}
